
package com.safebox.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String TAG = "DatabaseHelper";

    private static final int DATABASE_VERSION = 001;

    private static final String DATABASE_NAME = "message_keeper.db";

    private static DatabaseHelper sSingleton = null;

    public interface Tables {     
        public static final String MESSAGES = "Message";
        public static final String ACCOUNTS = "Account";
    }
    
    public interface MessagesColumns {
        public static final String MASSAGE_ID = BaseColumns._ID;
        public static final String TITLE = "title";
        public static final String CONTENT = "content";
        public static final String DATE_CREATED = "date_created";
        public static final String DATE_MODIFIED = "date_modified";  
        public static final String ACCOUT_ID = "account_id";
    }
    
    public interface AccountsColumns {
        public static final String ACCOUNT_ID = BaseColumns._ID;
        public static final String USERNAME = "username";
        public static final String PASSWORD = "pwd";
        public static final String DATE_CREATED = "date_created";
        public static final String DATE_MODIFIED = "date_modified"; 
    }

    private DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public static synchronized DatabaseHelper getInstance(Context context) {
        if (sSingleton == null) {
            sSingleton = new DatabaseHelper(context);
        }
        return sSingleton;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        /**Create the messages table.*/
        db.execSQL("CREATE TABLE " + Tables.MESSAGES + " (" +
                MessagesColumns.MASSAGE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                MessagesColumns.ACCOUT_ID + " INTEGER," +
                MessagesColumns.TITLE + " TEXT," +
                MessagesColumns.CONTENT + " TEXT," +
                MessagesColumns.DATE_CREATED + " TEXT," +
                MessagesColumns.DATE_MODIFIED + " TEXT" +                
        ");");
        
        /**Create the accounts table.*/
        db.execSQL("CREATE TABLE " + Tables.ACCOUNTS + " (" +
                AccountsColumns.ACCOUNT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                AccountsColumns.USERNAME + " TEXT," +
                AccountsColumns.PASSWORD + " TEXT," +
                AccountsColumns.DATE_CREATED + " TEXT," +
                AccountsColumns.DATE_MODIFIED + " TEXT" +
        ");");
        
        
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + Tables.MESSAGES + ";");
        db.execSQL("DROP TABLE IF EXISTS " + Tables.ACCOUNTS + ";");
        onCreate(db);

    }
    
    

}
